package cn.xshi.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.validation.Valid;
import cn.xshi.sys.form.SysNoticeForm;
import cn.xshi.sys.model.SysNotice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysNoticeService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
/**
 * @Desc 平台公告
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysNotice")
@Api(value = "平台公告API",tags = "平台公告API",description = "平台公告API")
public class SysNoticeController extends BaseAction {

	@Resource
	private SysNoticeService sysNoticeService;

	/**
	 * 查询平台公告列表并分页
	 * @param sysNoticeForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询平台公告列表并分页", notes="查询平台公告列表并分页")
	@GetMapping(value="/list")
	@Auth(value = "/sysNotice/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<SysNotice>> getSysNoticeListByCondition(SysNoticeForm sysNoticeForm,Integer pageNo, Integer pageSize){
		commonHPagerByPageNo(pageNo,pageSize);
		List<SysNotice> sysNoticeList = sysNoticeService.getSysNoticeListByCondition(sysNoticeForm);
		for(SysNotice sysNotice : sysNoticeList){
			if(!StringUtil.isEmpty(sysNotice.getCreateId())){
				OauthAccountEntity createBy = getAccount(sysNotice.getCreateId());
				if(null != createBy){
					sysNotice.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(sysNotice.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(sysNotice.getUpdateId());
				if(null != modifiedBy){
					sysNotice.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<SysNotice> page = new PageInfo<SysNotice>(sysNoticeList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	 * 查询单个平台公告
	 * @param id
	 */
	@ApiOperation(value="查询单个平台公告", notes="查询单个平台公告")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysNotice/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<SysNotice> getSysNoticeById(@PathVariable("id")String id){
		SysNotice sysNotice = sysNoticeService.getSysNoticeById(id);
		if(!StringUtil.isEmpty(sysNotice.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysNotice.getCreateId());
			if(null != createBy){
				sysNotice.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysNotice.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysNotice.getUpdateId());
			if(null != modifiedBy){
				sysNotice.setModifiedBy(modifiedBy.getName());
			}
		}
		return BaseResult.success(sysNotice);
	}

	/**
	 * 添加
	 * @param sysNotice
	 * @param bindingResult
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个平台公告", notes="创建单个平台公告")
	public BaseResult addSysNotice(@Valid @RequestBody SysNotice sysNotice, BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return outAudStr(false,backFem(bindingResult));
		}
		int i = 0;
		if(null != sysNotice){
			sysNotice.setId(toUUID());
			sysNotice.setCreateId(getCurrentAccountId());
			sysNotice.setCreateTime(getDate());
			i= sysNoticeService.addSysNotice(sysNotice);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 修改
	 * @param sysNotice
	 * @param bindingResult
	 */
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个平台公告", notes="编辑单个平台公告")
	public BaseResult updateSysNotice(@Valid @RequestBody SysNotice sysNotice, BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return outAudStr(false,backFem(bindingResult));
		}
		int i = 0;
		if(null != sysNotice){
			sysNotice.setUpdateId(getCurrentAccountId());
			sysNotice.setUpdateTime(getDate());
			i= sysNoticeService.updateSysNotice(sysNotice);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除平台公告", notes="删除平台公告")
	public BaseResult delSysNotice(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysNoticeService.delSysNotice(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 公告数
	 * @return
	 */
	@ApiOperation(value="公告数", notes="公告数")
	@GetMapping(value="/count")
	@Auth(value = "/sysNotice/count",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult count(){
		return outDataStr(sysNoticeService.getSysNoticeCountByCondition(new HashMap<>()));
	}
}
